<template>
  <view class="container">
    <view class="form-item">
      <text class="label">旧密码：</text>
      <input v-model="oldPassword" type="password" class="input" placeholder="请输入旧密码"/>
    </view>
    <view class="form-item">
      <text class="label">新密码：</text>
      <input v-model="newPassword" type="password" class="input" placeholder="请输入新密码"/>
    </view>
    <view class="form-item">
      <text class="label">确认新密码：</text>
      <input v-model="confirmPassword" type="password" class="input" placeholder="请确认新密码"/>
    </view>
    <view class="button-container">
      <button @click="changePassword" class="button">提交</button>
    </view>
  </view>
</template>

<script>
	 import config from '@/config/config.js'; // 引入全局配置文件
export default {
  data() {
    return {
      oldPassword: '',
      newPassword: '',
      confirmPassword: ''
    };
  },
  methods: {
    changePassword() {
		let USER_INFO = uni.getStorageSync('userInfo')
		if (this.oldPassword!=USER_INFO.password) {
			console.log("USER_INFO.password："+USER_INFO.password)
		  uni.showToast({
		    title: '您的旧密码输入错误',
		    icon: 'none'
		  });
		  return;
		}
      if (!this.oldPassword || !this.newPassword || !this.confirmPassword) {
        uni.showToast({
          title: '请填写完整信息',
          icon: 'none'
        });
        return;
      }
      
      if (this.newPassword !== this.confirmPassword) {
        uni.showToast({
          title: '新密码与确认密码不一致',
          icon: 'none'
        });
        return;
      }
	  //构建对象
	  let users = {
		  user_id:USER_INFO.user_id,
		  password:this.newPassword
	  }
      
      // 调用后端接口验证旧密码，并提交新密码
      // 这里只是示例，实际情况需要根据后端接口来实现
      // 假设后端接口地址为 /api/changePassword
      uni.request({
        url: config.baseURL + '/users/updatePwdByUserId',
        method: 'PUT',
        data: users,
        success: (res) => {
          if (res.data.status) {
            uni.showToast({
              title: '修改密码成功',
              icon: 'success'
            });
            // 清空输入框
            this.oldPassword = '';
            this.newPassword = '';
            this.confirmPassword = '';
			setTimeout(()=>{
				uni.reLaunch({
					url:'/pages/index/index',
					
				})
			},1000)
			
          } else {
            uni.showToast({
              title: res.data.message || '修改密码失败',
              icon: 'none'
            });
          }
        },
        fail: (err) => {
          console.error('修改密码失败', err);
          uni.showToast({
            title: '修改密码失败',
            icon: 'none'
          });
        }
      });
    }
  }
}
</script>

<style scoped>
.container {
  padding: 20px;
}

.form-item {
  margin-bottom: 20px;
}

.label {
  font-size: 16px;
  margin-right: 10px;
}

.input {
  width: calc(100% - 60px);
  padding: 10px;
  /* border: 1px solid #ccc; */
  border-radius: 5px;
  outline: none;
}

.button-container {
  display: flex;
  justify-content: center;
}

.button {
  background-color: #007AFF;
  color: #fff;
  border: none;
  border-radius: 5px;
  padding: 10px 20px;
  cursor: pointer;
  outline: none;
}
</style>
